1. Field of the Invention
In general, the present invention provides a computer-implemented method, system and program product for establishing multiple read-only locks on a shared data object. Specifically, the present invention allows a locking primitive for a shared data object to be automatically transformed to permit multiple read-only locks when certain conditions are met.
2. Related Art
A program which is executing using multiple threads requires synchronization points to ensure program correctness and proper operation, when two or more threads require access to shared data structures/objects. The synchronization is often achieved using locking primitives on a particular shared data object, where a single thread reserves exclusive access to the object at a given time. All other threads will wait to access the shared data object until the thread which has the access releases the lock, that is, stops using the shared data object in exclusive manner. After the thread releases the lock on the shared data object, the operating system will decide based on a particular algorithm which is the next thread that will be granted the exclusive access to the shared data object, if more than one thread is waiting on access to it. The synchronization points can be viewed as serialization primitives to ensure consistent values of the shared data object. The code fragment below, written in Java, presents two methods which are used to access a field in shared data object O, which is an instance of the class C, where they set and read the value of the field inside the object in thread safe manner:
class C {private int threadSafeField;public synchronized void set(int value) {threadSafeField = value;}public synchronized int get( ) {return threadSafeField;}}sample code fragment that uses the thread safe methods defined in the class C:
// in some method called foo( ). . .O.set(1);. . .// in some method called bar( ). . .int value = O.get( );. . .
The thread safe code in applications can often suffer from performance problems if it is not designed properly. Namely, the purpose of using multiple threads is often performance driven, in such a way that, on multiprocessor systems multiple operations can be executed at the same time. In such cases, the synchronization points serve as serialization primitives where all of the threads, which normally execute in parallel, have to access the shared data objects in order. Therefore if not used properly they can cause complete serialization of the program execution, as if there weren't multiple threads executing, and the program cannot exploit the multiple processors, if they are present on the system. This is often described as a scalability problem of the application.
One technique to alleviate this problem is to implement multiple reader-single writer locks for regions of the code where it is known that multiple threads can access shared data without danger of making it inconsistent. In particular, an inconsistent state of a shared data object can exist only if multiple threads are writing to it, or if they are doing reads and writes at the same time. Conversely, it is generally safe for all threads to read the shared data object if no one thread is writing to it. To exploit this fact and to improve scalability of the program, programmers manually implement special locking logic at portions of the program code where they allow multiple threads to perform read-only operations of the data object, while properly synchronizing in case a write operation is performed. Unfortunately, implementing such logic under current practices often requires careful engineering and is usually a tedious process, especially where there isn't lightweight support for it in the programming language, as it is the case with Java.
In view of the foregoing, there exists a need for a computer-implemented method, system, and program product for establishing multiple read-only locks on a shared data objects. Specifically, a need exists whereby a locking primitive associated with a shared data object can be automatically transformed to permit multiple read-only locks.